package base.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.ConvertUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.resource.gif.GifOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.signature.MediaStoreSignature;
import com.tange.ai.iot.client.gnssa.App;
import com.tange.ai.iot.client.gnssa.R;


import static com.bumptech.glide.load.DecodeFormat.PREFER_ARGB_8888;

/**
 * 说明：
 * Created by jjs on 2018/12/27
 */
public class GlideUtils {

    public static void loadCircleGif(Context context, Object url, ImageView mIvImg) {
        Glide.with(context)
                .setDefaultRequestOptions(new RequestOptions().set(GifOptions.DECODE_FORMAT, PREFER_ARGB_8888))
                .load(url)
                .into(mIvImg);
    }

    public static void loadUrl(Context context, Object url, ImageView mIvImg) {
        load(context, url, mIvImg, null, true);
    }

    public static void loadUrl(Context context, Object url, ImageView mIvImg, boolean isNoData) {
        load(context, url, mIvImg, null, true,isNoData);
    }

    public static void loadUrlHeadForVcoo(Context context, Object url, ImageView mIvImg) {
        String updateTime = String.valueOf(System.currentTimeMillis());
        Glide.with(context).load(url)/*.skipMemoryCache(true) // 不使用内存缓存
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)*/
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .signature(new MediaStoreSignature(updateTime, System.currentTimeMillis(), 0))
                .into(mIvImg);
    }


    public static void loadHeadUrl(Context context, Object url, ImageView mIvImg) {
        load(context, url, mIvImg, RequestOptions.circleCropTransform().placeholder(R.mipmap.ic_launcher), true);
    }

    public static void loadCircleUrl(Context context, Object url, ImageView mIvImg) {
        load(context, url, mIvImg, RequestOptions.circleCropTransform(), true);
    }

    public static void loadCircleUrlForVcoo(Context context, Object url, ImageView mIvImg) {
        load(context, url, mIvImg, RequestOptions.circleCropTransform(), true);
    }


    @SuppressLint("CheckResult")
    private static void load(Context context, Object url, ImageView mIvImg, RequestOptions options, boolean hasAllVisible, boolean isNoData) {
        RequestBuilder<Drawable> load = Glide.with(context).load(url);
        if (isNoData){
            load.placeholder(R.mipmap.ic_launcher)
                    .error(R.mipmap.ic_launcher);
        }
        if (options != null) {
            load.apply(options);
        }
        if (mIvImg != null) {
            if (!hasAllVisible && (TextUtils.isEmpty(url.toString()) || url.toString().endsWith("null"))) {
                mIvImg.setVisibility(View.GONE);
            }
            load.into(mIvImg);
        }
    }

    private static void load(Context context, Object url, ImageView mIvImg, RequestOptions options, boolean hasAllVisible) {
        RequestBuilder<Drawable> load = null;
        if (url == null || TextUtils.isEmpty(url.toString()) ) return;
        if ( url.toString().contains("http") && !url.toString().contains("?x-oss-process")) {

            if (App.compressLevel == 0) {
                load = Glide.with(context).load(url);
            } else if (App.compressLevel == 1) {
                load = Glide.with(context).load(url + "?x-oss-process=image/format,webp");
            } else if (App.compressLevel == 2){
                if(url.toString().contains("gif")){
                    load = Glide.with(context).load(url + "?x-oss-process=image/resize,m_lfit,h_480,w_480/format,webp");
                }else {
                    load = Glide.with(context).load(url + "?x-oss-process=image/resize,m_lfit,h_960,w_960/format,webp");
                }
            } else {
                load = Glide.with(context).load(url);
            }
        } else {
            load = Glide.with(context).load(url);
        }


        if (options != null) {
            load.apply(options);
        }
        if (mIvImg != null) {
            if (!hasAllVisible && (TextUtils.isEmpty(url.toString()) || url.toString().endsWith("null"))) {
                mIvImg.setVisibility(View.GONE);
            }
            load.into(mIvImg);
        }
    }
}
